package com.asset.dispoal.user.service.commom;

import com.asset.dispoal.commom.utils.AES;
import com.asset.dispoal.commom.utils.MD5;
import com.asset.dispoal.companyUser.domain.db.entity.SysCompanyUser;
import com.asset.dispoal.companyUser.domain.db.mapper.SysCompanyUserMapper;
import com.asset.dispoal.departmentUser.domain.db.entity.SysDepartmentUser;
import com.asset.dispoal.departmentUser.domain.db.mapper.SysDepartmentUserMapper;
import com.asset.dispoal.user.domain.db.entity.SysUserDo;
import com.asset.dispoal.user.domain.db.mapper.SysUserMapper;
import com.asset.dispoal.user.dto.UserRequest;
import com.asset.dispoal.user.service.converter.UserConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.sugar.Attempt;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

/**
 * @author qr
 * @date 2022/1/24$
 */
@Component
public class UserRegisterCommand implements BaseCommand<UserRequest, Result> {

    @Autowired
    BaseIdeable<String> snowFlake;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysCompanyUserMapper sysCompanyUserMapper;

    @Autowired
    SysDepartmentUserMapper sysDepartmentUserMapper;


    @Override
    @Transactional
    public Result<String> execute(UserRequest userRequest) {
        final String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        if (StringUtil.isBlank(userRequest.getNickname())) {
            userRequest.setNickname(userRequest.getAccount());
        }
        if (StringUtil.isBlank(userRequest.getAccount())) {
            return Result.faild("参数错误");
        }
        final String generateId = snowFlake.generateId();
        userRequest.setCreateUser(userId);
        userRequest.setCreateTime(DateTimeUtil.currentDate());
        userRequest.setReviewState("1");
        userRequest.setId(generateId);
        String salt = userRequest.getAccount().substring(userRequest.getAccount().length() - 2);
        String password = Attempt.supply(() -> MD5.getMd5Code(AES.encrypt(salt, userRequest.getPassword()), 1)).get();
        userRequest.setPassword(password);
        SysUserDo sysUserDo = UserConverter.INSTANCE.reqToDo(userRequest);
        sysUserMapper.insert(sysUserDo);
        SysCompanyUser sysCompanyUser = new SysCompanyUser();
        sysCompanyUser.setCompanyId(userRequest.getCompanyId());
        sysCompanyUser.setUserId(generateId);
        sysCompanyUserMapper.insert(sysCompanyUser);

        if (CollectionUtil.isNotEmpty(userRequest.getRoleIds())) {
            sysUserMapper.saveBatchUserRole(generateId, userRequest.getRoleIds());
        }

        SysDepartmentUser sysDepartmentUser = new SysDepartmentUser();
        sysDepartmentUser.setDepartmentId(userRequest.getDepIds());
        sysDepartmentUser.setUserId(generateId);
        sysDepartmentUserMapper.saveBatch(sysDepartmentUser);
        return Result.ok(generateId);
    }
}
